/*
 * Model
 * Root class to interact with database
 * Creates instances of model for basic SQL requests: select, modify, insert, delete
 */
package javaidiot;

import java.sql.*;

/**
 *
 * @author Master
 */
public class SQLConnection {

    protected String driver;
    protected String server;
    protected String props;
    protected static Connection connection = null;
    protected Select select;
    protected Insert insert;
    protected Update update;
    protected Delete delete;

    public SQLConnection(String driver, String server, String props) {
        this.driver = driver;
        this.server = server;
        this.props = props;
    }

    public void select(String query) {
        select = new Select(this.driver, this.server, this.props, query);
    }
    
    public void insert(String query) {
        insert = new Insert(this.driver, this.server, this.props, query);
    }
    
    public void update(String query) {
        update = new Update(this.driver, this.server, this.props, query);
    }
    
    public void delete(String query) {
        delete = new Delete(this.driver, this.server, this.props, query);
    }

    /*
     * Make string representation of the current time for debugging
     */
    private static String currentTime() {
        // Format the current time.
        java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
                "yyyy-MM-dd 'at' hh:mm:ss a zzz ':' ");
        return formatter.format(new java.util.Date());
    }

    /*
     * Debug
     */
    protected static void debug(String s) {
        s = currentTime() + s + "\n";
        System.out.print(s);
    }

    /*
     * Setter
     */
    public void setConnection() throws Exception {
        try {
            Class.forName(this.driver).newInstance();
            debug("DriverManager.getConnection(" + this.server + this.props);
            while (connection == null) {
                connection = DriverManager.getConnection(this.server
                        + this.props);
            }
            debug("Connection established.");
        } catch (ClassNotFoundException e) {
            debug("Database driver not found " + this.driver);
            throw (e);
        } catch (SQLException e) {
            SQLException e1 = e;
            while (e != null) {
                // there may be a pile of errors
                debug("SQL State =  " + e.getSQLState());
                // 08s01 = Communication link failure
                // 28000 = Invalid authorization specification
                debug(e.getMessage() + "Cannot connect to the database at "
                        + this.server);
                e = e.getNextException();
            }
            throw (e1);
        } catch (Exception e) {
            // catch anything else out there
            debug(e.getMessage());
        } 
    }

    /*
     * Getter
     */
    public Connection getConnection() {
        return connection;
    }
    
    /*
     * Checker
     */
    public boolean isConnected() {
        if (connection != null) {
            return true;
        } else {
            return false;
        }
    }
    
    /*
     * Destructor
     */
    public void close() {
        if (connection != null) {
            try {
                connection.close();
                debug("Connection closed");
            } catch (SQLException e) {
            }
        }
    }
}
